(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.custAcct.updateVerifyController', editController);
	editController.$inject = [ '$scope', '$q', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'custAcctService', 'storageService' ];
	function editController($scope, $q, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, custAcctService, storageService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.custAcct = {};
		ctrl.custAcctOld = {};
		custAcctService.initFunction(ctrl);
		
		ctrl.init = function() {
			custAcctService.initOptions().then(function(response){
				ctrl.custAcctTypeOptions = response.custAcctTypeOptions;
				ctrl.ccyOptions = response.ccyOptions;
				ctrl.paycodeCatOptions = response.paycodeCatOptions;
				ctrl.allPartyTypeOptions = response.allPartyTypeOptions;
				ctrl.yesNoOptions = response.yesNoOptions;
				ctrl.yesNoOptionsb = response.yesNoOptionsb;
				ctrl.swiftCaseList = response.swiftCaseList;
				ctrl.accountPatterns = response.accountPatterns;
				ctrl.accountPatternsTwo = response.accountPatternsTwo;
				
				ctrl.partyCountrys = response.countryOptionAll;
				ctrl.partyStates = response.stateOptionAll;
				ctrl.partyCitys = response.cityOptionAll;
				ctrl.stateList = response.stateList;
				ctrl.cityList = response.cityList;
				ctrl.partyStatesCatch = response.stateOptionAll;
				ctrl.partyCitysCatch = response.cityOptionAll;
							
				authService.updateVerify(params).then(function(response) {
					ctrl.custAcct = response.data.model.newModel || {};
					ctrl.custAcctOld = response.data.model.oldModel || {};
					ctrl.custAcct.taskId = response.data.task.taskId;
					ctrl.custAcct.siteCode = storageService.getUserInfo().siteCode;
					ctrl.custAcct.custAcctSwift = ctrl.custAcct.custAcctSwift || {};
					ctrl.custAcct.custAcctSwift.payPartyList = ctrl.custAcct.custAcctSwift.payPartyList || [];
					ctrl.custAcctOld.siteCode = storageService.getUserInfo().siteCode;
					ctrl.custAcctOld.custAcctSwift = ctrl.custAcctOld.custAcctSwift || {};
					ctrl.custAcctOld.custAcctSwift.payPartyList = ctrl.custAcctOld.custAcctSwift.payPartyList || [];
					ctrl.initPyPartyTabs();
					initPayPartyOldModel();
				});
			});
		};
		
		
		function initPayPartyOldModel(){
			ctrl.payPartyOldModel = {};
			if(ctrl.custAcctOld && ctrl.custAcctOld.custAcctSwift.payPartyList){
				angular.forEach(ctrl.custAcctOld.custAcctSwift.payPartyList, function(data, index, array){
					ctrl.payPartyOldModel[data.partyType] = data;
				});
			}
		};
				
		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.custAcct.comments = comments;
						authService.updateVerifyReject(ctrl.custAcct).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.custAcct.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.custAcct).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.custAcct.list',
					'state' : 'cda.custAcct.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.custAcct.list',
					'state' : 'cda.custAcct.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
